package com.jingfu.controller;

import cn.hutool.core.lang.Dict;
import com.jingfu.Service.DictService;
import com.jingfu.dto.DictDataDTO;
import com.jingfu.dto.DictTypeDTO;
import com.jingfu.util.CommonResult;
import com.jingfu.util.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

/**
 * @author jingfuu@163.com
 * @version v1.0
 * @date 2022/2/9 上午9:23
 **/
@Controller
@Slf4j
public class DictController {
    @Autowired
    DictService dictService;

    /**
     * 字典视图
     *
     * @return
     */
    @RequestMapping("/dict-view")
    public String getDictView() {
        return "system/dict";
    }

    /**
     * 字典类型新增视图
     * @return
     */
    @RequestMapping("/dict-type-add-view")
    public String getDictTypeAddView() {
        return "system/operate/dictTypeAdd";
    }

    /**
     * 字典类型修改视图
     * @return
     */
    @RequestMapping("/dict-type-edit-view")
    public String getDictTypeEidtView() {
        return "system/operate/dictTypeEdit";
    }

    /**
     * 字典数据新增视图
     * @return
     */
    @RequestMapping("/dict-data-add-view")
    public String getDictDataAddView() {
        return "system/operate/dictDataAdd";
    }

    /**
     * 字典数据修改视图
     * @return
     */
    @RequestMapping("/dict-data-edit-view")
    public String getDictDataEditView() {
        return "system/operate/dictDataEdit";
    }


    /**
     * 字典查询列表
     * @param dictTypeDTO
     * @return
     */
    @RequestMapping("/dict-type/list")
    @ResponseBody
    public CommonResult<List<DictTypeDTO>> getDictTypeList(DictTypeDTO dictTypeDTO) {
        log.info("请求查询字典类型数据列表，查询对象：{}", dictTypeDTO);
        PageUtil.startPage();
        List<DictTypeDTO> datas = dictService.getDictTypeList(dictTypeDTO);
        return PageUtil.buidPage(datas);
    }

    /**
     * 字典类型状态切换
     *
     * @param id
     * @param enable
     * @return
     */
    @RequestMapping("/dict-type/enable/{id}/{enable}")
    @ResponseBody
    public CommonResult dictTypeEnable(@PathVariable String id, @PathVariable String enable) {
        dictService.dictTypeEnable(id, enable);
        return CommonResult.ok().build();
    }

    /**
     * 字典删除
     * @param id
     * @return
     */
    @RequestMapping("/dict-type/delete/{id}")
    @ResponseBody
    public CommonResult deleteDictType(@PathVariable String id) {
        log.info("请求删除字典类型,字典类型id：{}",id);
        dictService.deleteDictType(id);
        return CommonResult.ok().build();
    }

    /**
     * 字典批量删除
     * @param ids
     * @return
     */
    @RequestMapping("/dict-type/batch-delete/{ids}")
    @ResponseBody
    public CommonResult batchDeleteDictType(@PathVariable String ids) {
        Arrays.stream(ids.split(",")).forEach(id -> {
            dictService.deleteDictType(id);
        });
        return CommonResult.ok().build();
    }

    /**
     * 字典创建&修改
     * @param dictTypeDTO
     * @return
     */
    @RequestMapping("/dict-type/create")
    @ResponseBody
    public CommonResult dictTypeCreate(@RequestBody DictTypeDTO dictTypeDTO) {
        log.info("请求创建字典类型，请求参数：{}",dictTypeDTO);
        dictService.dictTypeCreateOrUpdate(dictTypeDTO);
        return CommonResult.ok().build();
    }

    @RequestMapping("/dict-data/list")
    @ResponseBody
    public CommonResult<List<DictDataDTO>> getDictDataList(DictDataDTO dataDTO) {
        PageUtil.startPage();
        List<DictDataDTO> datas = dictService.getDictDataList(dataDTO);
        return PageUtil.buidPage(datas);
    }

    /**
     * 字典数据状态切换
     *
     * @param id
     * @param enable
     * @return
     */
    @RequestMapping("/dict-data/enable/{id}/{enable}")
    @ResponseBody
    public CommonResult dictDataEnable(@PathVariable String id, @PathVariable String enable) {
        dictService.dictDataEnable(id, enable);
        return CommonResult.ok().build();
    }

    /**
     * 字典数据删除
     * @param id
     * @return
     */
    @RequestMapping("/dict-data/delete/{id}")
    @ResponseBody
    public CommonResult deleteDataType(@PathVariable String id) {
        log.info("请求删除字典数据,字典数据id：{}",id);
        dictService.deleteDictData(id);
        return CommonResult.ok().build();
    }

    /**
     * 字典数据批量删除
     * @param ids
     * @return
     */
    @RequestMapping("/dict-data/batch-delete/{ids}")
    @ResponseBody
    public CommonResult batchDeleteDictData(@PathVariable String ids) {
        Arrays.stream(ids.split(",")).forEach(id -> {
            dictService.deleteDictData(id);
        });
        return CommonResult.ok().build();
    }


    /**
     * 字典数据创建&修改
     * @param dictDataDTO
     * @return
     */
    @RequestMapping("/dict-data/create")
    @ResponseBody
    public CommonResult dictTypeCreate(@RequestBody DictDataDTO dictDataDTO) {
        log.info("请求创建字典数据，请求参数：{}",dictDataDTO);
        dictService.dictDataCreateOrUpdate(dictDataDTO);
        return CommonResult.ok().build();
    }

}
